//
//  ChoiceView.m
//  EveryDay
//
//  Created by tidoo on 16/4/23.
//  Copyright © 2016年 Tidoo. All rights reserved.
//

#import "ChangeChoiceView.h"

@interface ChangeChoiceView ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *scrollView;

@property(nonatomic,strong)NSArray *titleArray;

@property(nonatomic,weak)UIButton *selectbutton;

@property(nonatomic,strong)NSMutableArray *buttonArray;

@property(nonatomic,weak)UIView *lineView;
//@property (nonatomic,assign) CGFloat buttonWidth;
@end

@implementation ChangeChoiceView

- (NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        _buttonArray=[NSMutableArray array];
    }
    return _buttonArray;
}
//- (instancetype)init
//{
//    if (self=[super init]) {
//        self.backgroundcolor=[UIColor whiteColor];
//        self.selectbackgroundcolor=[UIColor clearColor];
//    }
//    return self;
//}

- (instancetype)initWithFrame:(CGRect)frame andtitleArray:(NSArray *)titlearray{
    if (self=[super initWithFrame:frame]) {
        self.titleArray=titlearray;
        [self creatView];
        self.backgroundcolor=[UIColor whiteColor];
        self.selectbackgroundcolor=[UIColor whiteColor];
    }
    return self;
}
- (void)creatView{
    
//    self.buttonWidth = buttonW;
    
    CGFloat buttonH=CGRectGetHeight(self.bounds);
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(self.bounds))];
    scrollView.pagingEnabled=NO;
    scrollView.backgroundColor=[UIColor whiteColor];
    scrollView.delegate=self;
    scrollView.contentSize=CGSizeMake(CGRectGetWidth(self.bounds), 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView = scrollView;
    [self addSubview:scrollView];
    
    CGFloat buttonX = 10.0;
    for (int i=0; i<self.titleArray.count; i++) {
        CGFloat buttonW= [Global getSizeOfString:self.titleArray[i] maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width;
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(buttonX, 0, buttonW, buttonH)];
        [scrollView addSubview:button];
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        [button setTitle:self.titleArray[i] forState:UIControlStateSelected];
        [button setTitleColor:KTEXTMAINCOLOR forState:UIControlStateNormal];
        [button setTitleColor:KTEXTMAINCOLOR forState:UIControlStateSelected];
        button.titleLabel.font=[UIFont boldSystemFontOfSize:16];
        button.backgroundColor=[UIColor whiteColor];
        [button addTarget:self action:@selector(didselectbutton:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.textAlignment=NSTextAlignmentCenter;
        button.tag=i;
        
        buttonX = buttonX+buttonW + 10;
//        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(button.frame), 5, 1, buttonH - 10)];
//        lineView.backgroundColor=shouldgraycolor;
//        [self addSubview:lineView];

        [self.buttonArray addObject:button];
        if (!i) {
            button.selected=YES;
            self.selectbutton=button;
//            button.backgroundColor= KGRAYTEXTCOLOR;
        }
    }
    CGFloat buttonW= [Global getSizeOfString:self.titleArray[0] maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width;
    UIView *bottomlineView=[[UIView alloc]initWithFrame:CGRectMake(10+(buttonW - 20)/2, buttonH-5, 20, 5)];
    bottomlineView.backgroundColor=KBACKNAVIGATIONCOLOR;
    [self.scrollView addSubview:bottomlineView];
    self.lineView=bottomlineView;

}

- (void)didselectbutton:(UIButton *)sender{
    [self buttonclick:sender];
    if (self.clickbuttonblock) {
        self.clickbuttonblock(sender.tag);
    }
}

- (void)buttonclick:(UIButton *)sender{
    self.selectbutton.backgroundColor=self.backgroundcolor;
    self.selectbutton.selected=NO;
    sender.backgroundColor=self.selectbackgroundcolor;
    sender.selected=YES;
    self.selectbutton=sender;
    
    CGRect rect=sender.frame;
    rect.origin.y=rect.size.height - 5;
    rect.origin.x = (rect.size.width - 20)/2;
    rect.size.width=20.0;
    rect.size.height=5.0;
    [UIView animateWithDuration:1.0 animations:^{
//        self.lineView.transform=CGAffineTransformMakeTranslation(ButtonWidth, 0);
        self.lineView.x=sender.x+([Global getSizeOfString:sender.titleLabel.text maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width-20)/2;
    }];
//    [sender addSubview:self.lineView];
}
- (void)clickbuttonAtIndex:(NSInteger)index{
    UIButton *button=self.buttonArray[index];
    [self buttonclick:button];
    CGFloat contentwidth=self.scrollView.contentSize.width;
//    CGFloat buttonWidth=[Global getSizeOfString:button.titleLabel.text maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width;
    CGFloat curWidth = [Global getSizeOfString:button.titleLabel.text maxWidth:MAXFLOAT maxHeight:20 withFontSize:16.6].width + button.frame.origin.x+10;
    [UIView  animateWithDuration:1.0 animations:^{
        MyLog(@"%f,%f,%f,%f",button.frame.origin.x,kScreenW/2,contentwidth,contentwidth-kScreenW);
        if (curWidth<kScreenW/2 || contentwidth<kScreenW){
            self.scrollView.contentOffset=CGPointMake(0, 0);
        }else if (curWidth+kScreenW/2>contentwidth ) {
            self.scrollView.contentOffset=CGPointMake(contentwidth-kScreenW, 0);
        }else {
            self.scrollView.contentOffset=CGPointMake(curWidth-kScreenW/2, 0);
        }
    }];

    
}

- (void)setBackgroundcolor:(UIColor *)backgroundcolor
{
    _backgroundcolor=backgroundcolor;
    for (int i=0; i<self.buttonArray.count; i++) {
        if (i) {
            [(UIButton *)self.buttonArray[i] setBackgroundColor:backgroundcolor];
        }
    }
}

- (void)setSelectbackgroundcolor:(UIColor *)selectbackgroundcolor
{
    _selectbackgroundcolor=selectbackgroundcolor;
    [(UIButton *)self.buttonArray[0] setBackgroundColor:selectbackgroundcolor];
}

- (void)setTitlecolor:(UIColor *)titlecolor{
    _titlecolor=titlecolor;
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitleColor:titlecolor forState:UIControlStateNormal];
    }];
}

- (void)setSelecttitlecolor:(UIColor *)selecttitlecolor
{
    _selecttitlecolor=selecttitlecolor;
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj setTitleColor:selecttitlecolor forState:UIControlStateSelected];
    }];

}

- (void)setFontSize:(CGFloat)FontSize
{
    _FontSize=FontSize;
    [self.buttonArray enumerateObjectsUsingBlock:^(UIButton  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.titleLabel.font=[UIFont systemFontOfSize:FontSize];
    }];

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}

@end
